from django.test import TransactionTestCase
from django.utils import timezone

from delivery.service.delivery_generate import generate_and_save_delivery
from events.models import StoreProductReplenishment

class DeliveryGenerateTest(TransactionTestCase):
    def test_generate_and_save_delivery_success(self):
        """测试补货建议生成和保存的成功场景"""
        # 执行测试
        result = generate_and_save_delivery()

        # 验证结果
        self.assertTrue(result['success'])
        self.assertGreater(result['count'], 0)
        
        # 验证数据库记录
        replenishments = StoreProductReplenishment.objects.all()
        self.assertGreater(replenishments.count(), 0)
        
        # 验证第一条记录的字段
        first_replenishment = replenishments.first()
        self.assertIsNotNone(first_replenishment.order_id)
        self.assertIsNotNone(first_replenishment.order_date)
        self.assertIsNotNone(first_replenishment.product_code)
        self.assertFalse(first_replenishment.is_cancelled)
        
        # 验证补货计算字段
        self.assertIsNotNone(first_replenishment.avg_sales_quantity)
        self.assertIsNotNone(first_replenishment.safety_inventory_quantity)
        self.assertIsNotNone(first_replenishment.instock_quantity)
        self.assertIsNotNone(first_replenishment.dynamic_sales_days)
        self.assertIsNotNone(first_replenishment.safety_inventory_days)
